#include <iostream>
#include <string>
#include <vector>

using namespace std;

class Solution 
{
public:
    string reverseWords(string s) 
    {
        while(s[0] == ' ')
        {
            s = s.substr(1);
        }
        while (s[s.length() - 1] == ' ')
        {
            s.resize(s.length() - 1);
        }
        s += ' ';
        vector<string> wordsVector;
        string word;
        for(int i = 0; i < s.size(); i++)
        {
            if(s[i] == ' ')
            {
                if(s[i - 1] != ' ')
                {
                    wordsVector.push_back(word);
                    word.clear();
                }
                continue;
            }
            word += s[i];
        }

        string result;
        for(int i = 0; i < wordsVector.size(); i++)
        {
            result += wordsVector[wordsVector.size() - 1 - i];
            result += ' ';
        }
        result.resize(result.length() - 1);
        return result;
    }
};

int main()
{
    Solution sol;

    // 测试用例1
    string s1 = "the sky is blue";
    cout << "输入: \"" << s1 << "\"" << endl;
    cout << "输出: \"" << sol.reverseWords(s1) << "\"" << endl;
    // 期望输出: "blue is sky the"

    // 测试用例2
    string s2 = "  hello world  ";
    cout << "输入: \"" << s2 << "\"" << endl;
    cout << "输出: \"" << sol.reverseWords(s2) << "\"" << endl;
    // 期望输出: "world hello"

    // 测试用例3
    string s3 = "a good   example";
    cout << "输入: \"" << s3 << "\"" << endl;
    cout << "输出: \"" << sol.reverseWords(s3) << "\"" << endl;
    // 期望输出: "example good a"

    // 测试用例4
    string s4 = "  Bob    Loves  Alice   ";
    cout << "输入: \"" << s4 << "\"" << endl;
    cout << "输出: \"" << sol.reverseWords(s4) << "\"" << endl;
    // 期望输出: "Alice Loves Bob"

    // 测试用例5
    string s5 = "Alice does not even like bob";
    cout << "输入: \"" << s5 << "\"" << endl;
    cout << "输出: \"" << sol.reverseWords(s5) << "\"" << endl;
    // 期望输出: "bob like even not does Alice"

    return 0;
}
